using System; 
using UnityEngine;
using UnityEngine.Events;

namespace XFGameFramework.CommonSystem
{
     
    [Serializable]
    public class TriggerEvent : UnityEvent<Collider> { }
    [Serializable]
    public class TriggerEvent2D : UnityEvent<Collider2D> { }

    /// <summary>
    /// Trigger事件监听
    /// </summary>
    public class TriggerEventListener : MonoBehaviour
    {

        [SerializeField]
        private LayerMask layerMask;

        // Fix编码

        public TriggerEvent onTriggerEnter = new TriggerEvent();
        public TriggerEvent onTriggerStay = new TriggerEvent();
        public TriggerEvent onTriggerExit = new TriggerEvent();

        public TriggerEvent2D onTriggerEnter2D = new TriggerEvent2D();
        public TriggerEvent2D onTriggerStay2D = new TriggerEvent2D();
        public TriggerEvent2D onTriggerExit2D = new TriggerEvent2D();


        private void Reset() {
            layerMask = Physics.AllLayers;
        }

        private void OnTriggerEnter(Collider other)
        {
            if (!layerMask.Contains(other.gameObject.layer))
                return;
            onTriggerEnter?.Invoke(other);
        }

        private void OnTriggerStay(Collider other)
        {
            if (!layerMask.Contains(other.gameObject.layer))
                return;
            onTriggerStay?.Invoke(other);
        }

        private void OnTriggerExit(Collider other)
        {
            if (!layerMask.Contains(other.gameObject.layer))
                return;
            onTriggerExit?.Invoke(other);
        }

        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (!layerMask.Contains(collision.gameObject.layer))
                return;
            onTriggerEnter2D?.Invoke(collision);
        }

        private void OnTriggerStay2D(Collider2D collision)
        {
            if (!layerMask.Contains(collision.gameObject.layer))
                return;
            onTriggerStay2D?.Invoke(collision);
        }

        private void OnTriggerExit2D(Collider2D collision)
        {
            if (!layerMask.Contains(collision.gameObject.layer))
                return;
            onTriggerExit2D?.Invoke(collision);
        } 
    
    
    }
}